package com.example.flowable.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.annotations.Autowired;
import com.example.annotations.Service;
import com.example.db.DBSql;
import com.example.db.SqlBuilder;
import com.example.db.TableService;
import com.example.db.model.IPage;
import com.example.flowable.model.TableModel;
import com.example.flowable.model.vo.TableModelVo;
import com.example.flowable.service.TableModelService;
import com.example.flowable.utils.TableSqlUtils;
import liquibase.pro.packaged.L;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Classname TableModelServiceImpl
 * @Description TODO
 * @Date 2022/8/16 22:41
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@Service
public class TableModelServiceImpl implements TableModelService {

    @Autowired
    TableService tableService;

    @Autowired
    DBSql dbSql;

    @Override
    public IPage<TableModel> page(Integer pageNum, Integer pageSize, JSONObject object) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(TableModel.class);
        if(object != null){

        }
        sqlBuilder.orderByDesc("createdate");
        sqlBuilder.page(pageNum,pageSize);
        return tableService.getPage(sqlBuilder,TableModel.class);
    }

    @Override
    public TableModel save(TableModel model) {
        tableService.save(model,TableModel.class);
        return model;
    }

    @Override
    public TableModel updateById(TableModel model) {
        tableService.updateById(model,TableModel.class);
        return model;
    }

    @Override
    public TableModel getById(Object id) {
        TableModel model=tableService.getOneById(id,TableModel.class);
        return model;
    }

    @Override
    public void createTable(TableModelVo tableModelVo) {
        String sql=TableSqlUtils.createTable(tableModelVo);
        System.out.println("建表sql: "+sql);
        //建表
        dbSql.update(sql);
    }

    @Override
    public TableModel getTableModelByTableName(String tableName) {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(TableModel.class);
        sqlBuilder.and("tableName").eq(tableName);
        TableModel tableModel=tableService.selectOne(sqlBuilder,TableModel.class);
        return tableModel;
    }

    @Override
    public Integer saveData(String tableName, Map<String, Object> map) {
        TableModel tableModel=getTableModelByTableName(tableName);
        if(tableModel == null){
            return null;
        }
        String tableField=tableModel.getTableField();
        if(!"".equals(tableField)){
            List<String> string=JSONArray.parseArray(tableField,String.class);
            List<String> keys=new ArrayList<>();
            for (Map.Entry< String,Object> entry:map.entrySet()){
                String key=entry.getKey();
                if(!string.contains(key)){
                    if("processTitle".equals(key) || "processModelId".equals(key)
                            || "processdefid".equals(key)){
                        continue;
                    }
                    keys.add(key);
                }
            }
            common(map);
            dbSql.insert(tableName,map);
            String sqlId=String.format("select  LAST_INSERT_ID() from %s limit 1",tableName);
            int id=dbSql.selectCount(sqlId);
            return id;
        }
        return null;
    }

    @Override
    public Integer updateDataById(String tableName, Map<String, Object> map) {
        map.remove("createdate");
        boolean flag=dbSql.update(tableName,map);
        if (flag) {
            return 1;
        }
        return 0;
    }

    @Override
    public Map<String, Object> getData(String tableName, Object id) {
        String sql=String.format("select  * from %s where id='%s' limit 1",tableName,id);
        Map<String,Object> map=dbSql.selectMaps(sql);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object data = entry.getValue();
            if (data instanceof Date) {
                String time=df.format(data);
                map.put(key,time);
            }
        }
        return map;
    }

    @Override
    public List<TableModel> getTableModelList() {
        SqlBuilder sqlBuilder=tableService.getSqlBuilder(TableModel.class);
        sqlBuilder.orderByDesc("createdate");
        List<TableModel> tableModels=tableService.selectList(sqlBuilder,TableModel.class);
        return tableModels;
    }

    private void common(Map<String,Object> object){
        if(object==null){
            return;
        }
        object.put("createdate",new Date());
        object.put("updatedate",new Date());
    }
}
